
package com.xm.test;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DragListActivity extends ListActivity {
    private ListView mExampleList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mExampleList = getListView();
        mExampleList.setOnCreateContextMenuListener(this);
//        ((ThirdListView) mExampleList).setDropListener(mDropListener);
        ((ThirdListView) mExampleList).setOnOrderChangedListener(mOnOrderChangedListener);
//        mExampleList.setAdapter(new ExampleArrayAdapter(getApplicationContext(),
//                R.layout.rowexample, new String[] {
//                        "Row 1", "Row 2", "Row 3",
//                        "Row 4", "Row 5", "Row 6",
//                        "Row 7", "Row 8", "Row 9",
//                        "Row 10", "Row 11", "Row 12"
//                }));
        SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.item,
                new String[] {
                    "title"
                }, new int[] {
                    R.id.item_text
                });
        mExampleList.setAdapter(adapter);
        mExampleList.setOnItemClickListener(mItemClickListener);
        mExampleList.setOnItemLongClickListener(mItemLongClickListener);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i< 12; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "Row " + i);
            list.add(map);
        }
        return list;
    }

    // ArrayAdapter
    private class ExampleArrayAdapter extends ArrayAdapter<String> {
        private Context mContext;
        private int mLayoutId;
        private String[] mListContent;

        public ExampleArrayAdapter(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
            mContext = context;
            mLayoutId = textViewResourceId;
            mListContent = objects;
        }

        @Override
        public View getView(int position, View rowView, ViewGroup parent) {
            if (rowView != null) {
                return rowView;
            }
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View v = inflater.inflate(mLayoutId, null);

            TextView rowTitle = (TextView) v.findViewById(R.id.item_text);
            rowTitle.setText(mListContent[position]);

            return v;
        }

    }

    private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(getApplicationContext(), "a long click " + mExampleList.getChildCount(), Toast.LENGTH_SHORT).show();
            ViewGroup item = (ViewGroup) (mExampleList.getChildAt(arg2 - mExampleList.getFirstVisiblePosition()));
            int y = item.getTop();
//            item.setDrawingCacheEnabled(true);
//            Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
//            item.setAlpha(0);
            ((ThirdListView) mExampleList).startDragging(item, y);
            return true;
        }
    };

    private OnItemClickListener mItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(getApplicationContext(), "a click: " + mExampleList.getItemAtPosition(arg2) , Toast.LENGTH_SHORT).show();
        }
    };
    // Drop Listener
//    private OtherListView.DropListener mDropListener = new OtherListView.DropListener() {
//        public void drop(int from, int to) {
//        }
//    };
    private ThirdListView.OnOrderChangedListener mOnOrderChangedListener = new ThirdListView.OnOrderChangedListener() {
        @Override
        public void OnOrderChanged(int from, int to) {
        }
      
  };
}
